package com.zw.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.zw.core.domain.AjaxResult;
import com.zw.dto.DirectoryDto;
import com.zw.entity.Directory;
import com.zw.service.DirectoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@Tag(name = "目录")
@RestController("userDirectoryController")
@RequestMapping("/user/directory")
public class DirectoryController {

    @Resource
    private DirectoryService directoryService;

    @Operation(summary = "查询用户目录列表")
    @GetMapping("/list")
    @PreAuthorize("@ss.authenticated()")
    public AjaxResult directoryList(DirectoryDto directoryDto) {
        return AjaxResult.success(directoryService.directoryList(directoryDto));
    }

    @Operation(summary = "新增目录")
    @PostMapping
    @PreAuthorize("@ss.authenticated()")
    public AjaxResult addDirectory(@RequestBody DirectoryDto directoryDto) {
        Directory directory = BeanUtil.copyProperties(directoryDto, Directory.class);
        directoryService.save(directory);
        return AjaxResult.success();
    }

    @Operation(summary = "删除目录")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.authenticated()")
    public AjaxResult deleteDirectory(@PathVariable Long id) {
        directoryService.removeDirectory(id);
        return AjaxResult.success();
    }

    @Operation(summary = "修改目录")
    @PutMapping
    @PreAuthorize("@ss.authenticated()")
    public AjaxResult updateDirectory(@RequestBody Directory directory) {
        directoryService.updateById(directory);
        return AjaxResult.success();
    }

}
